﻿var userId_;

function init() {
    comForm.bind(null, 'OUTHELP', { data: DataDict['data_outhelp'], textField: 'text', valueField: 'value' }, 3);
    comForm.bind(null, 'TEAM_STS', { data: DataDict['data_team_sts'], textField: 'text', valueField: 'value' }, 3);
    comForm.bind(null, 'PRO_STS', { data: DataDict['data_pro_sts'], textField: 'text', valueField: 'value' }, 3);

    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "GetCurrentUser",
        success: function (data) {
            if (data) {
                userId_ = data.USER_ID;

            }
        }
    });

    InitSelect();
    loadOrgData();
   
   
    if ($.getPara('teamid') != '') {
        initteam();
    }
}


function InitSelect() {

    //绑定项目级别
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryDictItemList2",
        model: function () { return 1003; },
        success: function (data) {
            if (data) {
                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    _data.push({ "text": data[i].ITEMNAME, "id": data[i].ITEMVALUE });
                }
                $('#PRO_LEVEL').combobox('loadData', _data);
            }
        }
    });

    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryUserInfoList",
        model: function () {
            var obj = {};
            return $.toJSON(obj);
        },
        success: function (data) {
            if (data) {
                var _data = [];
                for (var i = 0; i < data.length; i++) {

                    _data.push({ "text": data[i].USER_NAME, "id": data[i].USER_ID });
                }
                $('#TEAM_LEADER').combobox('loadData', _data);
                $('#RES_MANAGE').combobox('loadData', _data); //
                $('#PRO_LEADER').combobox('loadData', _data); //

            }
        }
    });


    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryTeamList_",
        model: function () {
           
        },
        success: function (data) {
            if (data) {
                var _data = [];
                for (var i = 0; i < data.length; i++) {

                    _data.push({ "text": data[i].TEAM_NAME, "id": data[i].TEAM_ID });
                }
                $('#FAR_PRO').combobox('loadData', _data);
            }
        }
    });


    //绑定项目类别
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryDictItemList2",
        model: function () { return 1002; },
        success: function (data) {
            if (data) {
                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    _data.push({ "text": data[i].ITEMNAME, "id": data[i].ITEMVALUE });
                  }
                $('#PRO_TYPE').combobox('loadData', _data);
            }
        }
    });



   


    //绑定专业类型
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryDictItemList2",
        model: function () { return 1004; },
        success: function (data) {
            if (data) {
                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    _data.push({ "text": data[i].ITEMNAME, "id": data[i].ITEMVALUE });
                }
                $('#PROFE_TYPE').combobox('loadData', _data);
            }
        }
    });


}




//#region 加载承建单位
function loadOrgData() {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryOrganizeList",
        success: function (data) {
            if (data && data.length > 0) {

                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PARENTID == '0') {
                        var obj = {};
                        obj.id = data[i].ORGANIZATION_ID;

                        obj.text = data[i].ORGANIZATION_NAME;
                        getsubOrg(data, data[i].ORGANIZATION_ID, obj);
                        _data.push(obj);
                    }
                }


                $('#ROLES_ID').combotree({ cascadeCheck: false });
                $('#ROLES_ID').combotree('loadData', _data);


            }

        }
    });

    function getsubOrg(_d, _pid, obj2) {
        if (_d && _d.length > 0) {
            var _sdata = [];
            for (var i = 0; i < _d.length; i++) {
                if (_d[i].PARENTID == _pid) {
                    var obj = {};
                    obj.id = _d[i].ORGANIZATION_ID;
                    obj.text = _d[i].ORGANIZATION_NAME;
                    _sdata.push(obj);
                }
            }
            obj2.children = _sdata;
            if (_sdata.length > 0) {
                for (var j = 0; j < _sdata.length; j++) {
                    getsubOrg(_d, _sdata[j].id, _sdata[j]);
                }
            }
        }
    }
}


//#endregion



//#region 加载角色数据
function loadRoleData() {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "RolesList",
        success: function (data) {
            if (data && data.length > 0) {
                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PARENTID == '0') {
                        var obj = {};
                        obj.id = data[i].ROLES_ID;
                        obj.text = data[i].ROLES_NAME;
                        obj.SORTCODE = data[i].SORTCODE;
                        obj.CREATEUSERNAME = data[i].CREATEUSERNAME;
                        obj.CREATEDATE = data[i].CREATEDATE;
                        obj.MODIFYUSERNAME = data[i].MODIFYUSERNAME;
                        obj.MODIFYDATE = data[i].MODIFYDATE
                        obj.ROLE_RESTRICTION = data[i].ROLE_RESTRICTION;
                        obj.DELETEMARK = data[i].DELETEMARK;
                        getsubRole(data, data[i].ROLES_ID, obj);
                        _data.push(obj);
                    }
                }
                $('#ORLEList').tree('loadData', _data);
                bindroleright();
            }

        }
    });

    function getsubRole(_d, _pid, obj2) {
        if (_d && _d.length > 0) {
            var _sdata = [];
            for (var i = 0; i < _d.length; i++) {
                if (_d[i].PARENTID == _pid) {
                    var obj = {};
                    obj.id = _d[i].ROLES_ID;
                    obj.DELETEMARK = _d[i].DELETEMARK;
                    obj.text = _d[i].ROLES_NAME;
                    obj.ROLE_RESTRICTION = _d[i].ROLE_RESTRICTION;
                    obj.CREATEUSERNAME = _d[i].CREATEUSERNAME;
                    obj.CREATEDATE = _d[i].CREATEDATE;
                    obj.MODIFYUSERNAME = _d[i].MODIFYUSERNAME;
                    obj.MODIFYDATE = _d[i].MODIFYDATE
                    obj.SORT_CODE = _d[i].SORT_CODE;
                    _sdata.push(obj);
                }
            }
            obj2.children = _sdata;
            if (_sdata.length > 0) {
                for (var j = 0; j < _sdata.length; j++) {
                    getsubRole(_d, _sdata[j].ROLES_ID, _sdata[j]);
                }
            }
        }
    }
}

//#endregion

function initteam() {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryTeamInfoById",
        model: function () {
            return $.getPara('teamid');
        },
        success: function (data) {
            if (data) {
                for (var key in data) {
                    if ($('#' + key).length > 0) {
                        if (key == 'ROLES_ID') {
                            // continue;
                            _orgdata = data[key];

                            if (_orgdata && _orgdata.length > 0) {
                                var _dataorgarr = [];
                                //                                for (var i = 0; i < _orgdata.length; i++) {
                                //                                  _dataorgarr.push(_orgdata[i].ORGANIZATION_ID);
                                //                                   }
                                _dataorgarr.push(data[key]);
                                $('#ROLES_ID').combotree('setValues', _dataorgarr);
                            }


                        }
                        else if (key == 'START_DATE' || key == 'END_DATE') {
                            var dates = (String)(data[key]);
                            dates = dates.substring(0,10);
                            if (data[key] && data[key].indexOf('1000-01-01') == -1) {
                                $('#' + key).datebox('setValue', dates);
                            }
                        }
                        else if ($('#' + key).attr("data-options")) {
                            $('#' + key).combobox('setValue', data[key]);
                        } else {

                            if (key == "OUTHELP" || key == "TEAM_STS" || key == "PRO_STS") {
                                $('#' + key).val(Number(data[key]));
                            }
                            else {
                                $('#' + key).val(data[key]);
                            }

                        }
                    }
                }
            }
        }
    });
}

//#region 编辑
function savecheck() {

    if ($('#TEAM_NAME').val() == '') {
        alert(lang.team_name_can_not_be_empty);
        return false;
    } else if ($('#TEAM_LEADER').combobox('getValue') == '') {
        alert(lang.project_leader_can_not_be_empty);
        return false;
    }
    else if ($('#START_DATE').combobox('getValue') == '') {
        alert(lang.start_date_can_not_be_empty);
        return false;
    }
    else if ($('#END_DATE').combobox('getValue') == '') {
        alert(lang.end_date_can_not_be_empty);
        return false;
    }
    if ($('#ROLES_ID').combotree('getValues') == 0) {
        alert(lang.Cannot_select_the_root_node);
        return false;
    }
   
    var str_START_DATE = $('#START_DATE').datebox('getValue');
    var arr1 = str_START_DATE.split("-");
    var date1 = new Date(arr1[0], parseInt(arr1[1]) - 1, arr1[2]);
    var str_END_DATE = $('#END_DATE').datebox('getValue');
    var arr2 = str_END_DATE.split("-");
    var date2 = new Date(arr2[0], parseInt(arr2[1]) - 1, arr2[2]);
    if (date1 > date2) {
        alert(lang.Start_date_cannot_be_greater_than_end_date);
        return false;
    }
    return true;
}
function save() {

    if ($.getPara('teamid') != '') {
        update();
        return;
    }
   
    if (savecheck()) {
        $.ajaxHandler({
            url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
            urlType: "addTeam",
            model: function () {
                var obj = {};
                if ($.getPara('teamid') != '') {
                    obj.TEAM_ID = $.getPara('teamid');
                    // obj.action = 1;
                }
                else {
                    // obj.action = 2;
                }

                obj.TEAM_NAME = $('#TEAM_NAME').val();
                obj.CREATE_USER = userId_; //输出 kingwell   "110";
                obj.TEAM_LEADER = $('#TEAM_LEADER').combobox('getValue');
                obj.TEAM_DESC = $('#TEAM_DESC').val();
                if ($('#ROLES_ID').combotree('getValues').length > 0) {
                    obj.ROLES_ID = $('#ROLES_ID').combotree('getValues')[0];
                }
                obj.TEAM_STS = $('#TEAM_STS').val();
                obj.OUTHELP = $('#OUTHELP').val();
                obj.PRO_TYPE = $('#PRO_TYPE').combobox('getValue');
                obj.FAR_PRO = $('#FAR_PRO').combobox('getValue');
                obj.RES_MANAGE = $('#RES_MANAGE').combobox('getValue');
                obj.PRO_LEVEL = $('#PRO_LEVEL').combobox('getValue');
                obj.PROFE_TYPE = $('#PROFE_TYPE').combobox('getValue');
                obj.HEL_CONTR = $('#HEL_CONTR').val();
                obj.PRO_LEADER = $('#PRO_LEADER').combobox('getValue');
                obj.ADJUS_INFO = $('#ADJUS_INFO').val();
                obj.EXPERT_GROUP = $('#EXPERT_GROUP').val();
                obj.DELAY_INFO = $('#DELAY_INFO').val();
                obj.PRO_STS = $('#PRO_STS').val();
                obj.PRO_FUND = $('#PRO_FUND').val();
                obj.PRO_EVA = $('#PRO_EVA').val();
                obj.START_DATE = $('#START_DATE').datebox('getValue');
                obj.END_DATE = $('#END_DATE').datebox('getValue');
                return $.toJSON(obj);
            },
            success: function (data) {
                if (data) {
                    if ($.getPara('teamid') != '') {
                        // $.alert(lang.Finish_editing_workgroup_information, null, function () { parent.teamlist.search(); parent.$.closePage(); });

                        alert(lang.Finish_editing_workgroup_information);
                        parent.teamlist.search();
                        parent.$.closePage();

                    } else {
                        //$.alert(lang.Finish_add_new_workgroup_information, null, function () { parent.teamlist.search(); parent.$.closePage(); });
                        alert(lang.Finish_add_new_workgroup_information);
                        parent.teamlist.search();
                        parent.$.closePage();
                    }
                   
                }
            }
        });
    }
}



function update() {
    if (savecheck()) {
        $.ajaxHandler({
            url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
            urlType: "UpdateTeam",
            model: function () {
                var obj = {};
                if ($.getPara('teamid') != '') {
                    obj.TEAM_ID = $.getPara('teamid');
                    // obj.action = 1;
                }
                else {
                    // obj.action = 2;
                }
                obj.TEAM_NAME = $('#TEAM_NAME').val();
                // obj.CREATE_USER = userId_;
                obj.TEAM_LEADER = $('#TEAM_LEADER').combobox('getValue');
                obj.TEAM_DESC = $('#TEAM_DESC').val();
                if ($('#ROLES_ID').combotree('getValues').length > 0) {
                    obj.ROLES_ID = $('#ROLES_ID').combotree('getValues')[0];
                }
                obj.OUTHELP = $('#OUTHELP').val();
                obj.PRO_TYPE = $('#PRO_TYPE').combobox('getValue');
                obj.FAR_PRO = $('#FAR_PRO').combobox('getValue');
                obj.RES_MANAGE = $('#RES_MANAGE').combobox('getValue');
                obj.PRO_LEVEL = $('#PRO_LEVEL').combobox('getValue');
                obj.PROFE_TYPE = $('#PROFE_TYPE').combobox('getValue');
                obj.HEL_CONTR = $('#HEL_CONTR').val();
                obj.PRO_LEADER = $('#PRO_LEADER').combobox('getValue');
                obj.ADJUS_INFO = $('#ADJUS_INFO').val();
                obj.EXPERT_GROUP = $('#EXPERT_GROUP').val();
                obj.DELAY_INFO = $('#DELAY_INFO').val();
                obj.PRO_STS = $('#PRO_STS').val();
                obj.TEAM_STS = $('#TEAM_STS').val();
                obj.PRO_FUND = $('#PRO_FUND').val();
                obj.PRO_EVA = $('#PRO_EVA').val();
                obj.START_DATE = $('#START_DATE').datebox('getValue');
                obj.END_DATE = $('#END_DATE').datebox('getValue');
                return $.toJSON(obj);
            },
            success: function (data) {

                if (data) {
                    if ($.getPara('teamid') != '') {
                        // $.alert(lang.Finish_editing_workgroup_information, null, function () { parent.teamlist.search(); parent.$.closePage(); });

                        alert(lang.Finish_editing_workgroup_information);
                        parent.teamlist.search();
                        parent.$.closePage();

                    } else {
                        //$.alert(lang.Finish_add_new_workgroup_information, null, function () { parent.teamlist.search(); parent.$.closePage(); });
                        alert(lang.Finish_add_new_workgroup_information);
                        parent.teamlist.search();
                        parent.$.closePage();
                    }

                }
            }
        });
    }
}




function exit() {
    parent.$.closePage();
}
//#endregion

//#region 角色
function bindroleright() {
    if ($.getPara('userid') != '') {
        $.ajaxHandler({
            url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
            urlType: "QueryUserRole",
            model: function () {
                return $.getPara('userid');
            },
            success: function (data) {
                if (data && data.length > 0) {
                    //绑定check
                    for (var i = 0; i < data.length; i++) {
                        var node = $('#ORLEList').tree('find', data[i].ROLES_ID);
                        $('#ORLEList').tree('check', node.target);
                    }
                }
            }
        });

    }
}
//#endre